﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace MessageSystem
{
    /// <summary>
    /// 消息管理器
    /// </summary>
    public class MessageManager : IMassageManager
    {
        /// <summary>
        /// 消息类型与监听它的消息终端的映射
        /// </summary>
        Dictionary<string, List<IMessageTerminal>> messageType2MessageTerminal = new Dictionary<string, List<IMessageTerminal>>();

        /// <summary>
        /// 弹出一个消息终端
        /// </summary>
        /// <param name="go"></param>
        /// <returns></returns>
        public IMessageTerminal PopMessageTerimal(GameObject go)
        {
            return go.AddComponent<MessageTerminal>();
        }

        public IMessageTerminal GetMessageTerimalByID(int id)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// 注册消息类型
        /// </summary>
        /// <param name="messageType"></param>
        /// <param name="messageTerminal"></param>
        public void RegisterMessage(string messageType, IMessageTerminal messageTerminal)
        {
            if (messageType2MessageTerminal.ContainsKey(messageType))
            {
                if (!messageType2MessageTerminal[messageType].Contains(messageTerminal))
                {
                    messageType2MessageTerminal[messageType].Add(messageTerminal);
                }
                else
                {
                    DebugUtils.LogRedInfo("同一个消息类型重复注册消息终端");
                }
            }
            else
            {
                messageType2MessageTerminal.Add(messageType, new List<IMessageTerminal>() { messageTerminal });
            }
        }

        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="messageType"></param>
        /// <param name="args"></param>
        public void SendMessage(string messageType, IMessageArgs args)
        {
            args.MessageType = messageType;
            if (messageType2MessageTerminal.ContainsKey(messageType))
            {
                for (int i = 0; i < messageType2MessageTerminal[messageType].Count; i++)
                {
                    messageType2MessageTerminal[messageType][i].ReceiveMessage(messageType, args);
                }
            }
        }

        /// <summary>
        /// 消息注销
        /// </summary>
        /// <param name="messageType"></param>
        /// <param name="messageTerminal"></param>
        public void UnRegisterMessage(string messageType, IMessageTerminal messageTerminal)
        {
            if (messageType2MessageTerminal.ContainsKey(messageType))
            {
                if (messageType2MessageTerminal[messageType].Contains(messageTerminal))
                {
                    messageType2MessageTerminal[messageType].Remove(messageTerminal);
                    return;
                }
            }
            DebugUtils.LogRedInfo("注销失败");
        }

        /// <summary>
        /// 注销消息终端
        /// </summary>
        /// <param name="messageTerminal"></param>
        public void UnRegisterMessageTerminal(IMessageTerminal messageTerminal)
        {
            for (int i = 0; i < messageTerminal.Messages.Length; i++)
            {
                if (messageType2MessageTerminal[messageTerminal.Messages[i]].Contains(messageTerminal))
                {
                    messageType2MessageTerminal[messageTerminal.Messages[i]].Remove(messageTerminal);
                    return;
                }
            }
        }
    }
}

